﻿using System;
using Wei8808Cms.BLL;
using Wei8808Cms.Model;
using Wei8808Cms.WebSite.UI;

namespace Wei8808Cms.WebSite.admin.Administrator
{
    public partial class EditAdministratorInfoForm : BasicPage
    {
        public string Id;
        protected string strLevel;
        protected int strType;
        private readonly AdministratorBLL _bll = new AdministratorBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            chkLoginLevel("editAdministrator");
            if (string.IsNullOrEmpty(Request.Params["id"]))
            {
                JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                return;
            }
            Id = Request.Params["id"];
            if (Page.IsPostBack) return;
            ShowInfo(this.Id);
        }

        private void ShowInfo(string id)
        {
            var model = _bll.GetAdministratorInfo(id);
            txtUserName.Text = model.UserName;
            if (model.IsLock == 1)
            {
                this.rblIsLock.Items[1].Selected = true;
            }
            else
            {
                this.rblIsLock.Items[0].Selected = true;
            }
            txtReadName.Text = model.ReadName;
            txtUserEmail.Text = model.UserEmail;
            this.strLevel = model.UserLevel;
            this.strType = model.UserType;
            if (model.UserType == 1)
            {
                this.rblUserType.Items[0].Selected = true;
            }
            if (model.UserType == 2)
            {
                this.rblUserType.Items[1].Selected = true;
            }
            if (model.UserType == 3)
            {
                this.rblUserType.Items[2].Selected = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var model = new AdministratorInfo();
            string userLevel = string.Empty;
            int userType = Convert.ToInt32(rblUserType.SelectedValue);
            if (userType > 1)
            {
                userLevel = "," + Request.Form["cbLevel"].Trim() + ",";
            }
            model.Id = this.Id;
            model.UserName = txtUserName.Text.Trim();
            model.UserPwd = txtUserPwd.Text.Trim();
            model.ReadName = txtReadName.Text.Trim();
            model.UserEmail = txtUserEmail.Text.Trim();
            model.UserType = userType;
            model.IsLock = Convert.ToInt32(rblIsLock.SelectedValue);
            model.UserLevel = userLevel;
            model.ModifyUser = "李波";
            model.ModifyTime = DateTime.Now;
            _bll.Update(model);
            JscriptPrint("修改成功！", "AdministratorMasterForm.aspx", "Success");
        }
    }
}